package com.ogawa.chair7808.observer;

import com.ogawa.base.network.bean.BeanUpdata;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by FHB on 2018/4/18.
 */
public class UpdataObserver {

    private static UpdataObserver mInst = null;
    private static Object mLock = new Object();

    private List<OnUpdatasterner> mOnUpdataListerner;

    private UpdataObserver(){
        mOnUpdataListerner = new ArrayList<OnUpdatasterner>();
    }

    public static UpdataObserver getInst(){
        if(mInst == null){
            synchronized(mLock){
                if(mInst == null){
                    mInst = new UpdataObserver();
                }
            }
        }
        return mInst;
    }

    public void  registerObserver(OnUpdatasterner action){
        if(!mOnUpdataListerner.contains(action)){
            mOnUpdataListerner.add(action);
        }
    }

    public void removeObserver(OnUpdatasterner action){
        mOnUpdataListerner.remove(action);
    }


    public void noticeonUpdata(BeanUpdata bean){
        for (int i = 0; i < mOnUpdataListerner.size(); i++) {
            try {
                mOnUpdataListerner.get(i).toUpdata(bean);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public interface OnUpdatasterner {
        //连接状态
        void toUpdata(BeanUpdata bean);


    }


}
